package com.skyon.rule.res.model;

import java.io.Serializable;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public final class InputUrlAdapter  implements Serializable{
	private static final long serialVersionUID = 1L;
	
	private static final String EMPTY = "";
	private static final String ROOT_SEPARATOR = "/";
	private static final String RETURN_SEPARATOR = "?return=";
	
	private static final String CLASS_EXTENSION_NAME = ".class";
	
	private String root;
	private String processID;
	private String[] retObjs;
	
	
	private static Logger logger = LoggerFactory.getLogger( InputUrlAdapter.class );


	public InputUrlAdapter(String root, String processID, String... retObjs) {
		this.root = root;
		this.processID = processID;
		this.retObjs = retObjs;
	}

	public InputUrlAdapter(String root, String processID) {
		this.root = root;
		this.processID = processID;
		this.retObjs = new String[0];
	}
	
	/**
	 * ruleURL 如 com.ccb.tfs.n2fs/sample?return=OutBean1,OutBean2
	 *               访问sample规则流
	 *               默认返回对象 OutBean1.propName;...;OutBean2.propName 形式
	 *           com.ccb.tfs.n2fs/?return=OutBean
	 *               访问默认主规则流
	 *               默认返回对象 OutBean.propName 形式
	 *           com.ccb.tfs.n2fs/sample?
	 *               访问sample规则流
	 *               默认返回所有对象 OutBeanx.propName 形式
	 *           com.ccb.tfs.n2fs/
	 *               访问默认主规则流
	 *               默认返回所有对象 OutBeanx.propName 形式
	 * 其中/ 有且只有一个，是判断是否合法的标准
	 * 要求
	 *   使用部署的主文件名（不要扩展名）作为规则访问的域名
	 *   调用者的规则流文件名和Process ID一致
	 * 
	 * @param ruleURL
	 * @throws Exception
	 */
	public InputUrlAdapter(String ruleURL) throws Exception {
		
		logger.debug("the plot URL is: " + ruleURL);
		
		if (! vaildURL(ruleURL)) throw new Exception("bad URL found exception");
		
		String root = StringUtils.substringBefore(ruleURL, ROOT_SEPARATOR);
		if ( root == null || root.equals(EMPTY) || root.length() == 0 ) {
			 throw new Exception("bad URL found exception");
		}
		
		String processID = null;
		String[] outClsArr = null;
		
		String outClsesNames = StringUtils.substringAfter(ruleURL, RETURN_SEPARATOR);
		if (notNullValues(outClsesNames)) {
			processID = StringUtils.substringBetween(ruleURL, ROOT_SEPARATOR, RETURN_SEPARATOR);
			outClsArr = StringUtils.split(outClsesNames, ",");
			
		}else{
			processID = StringUtils.substringAfter(ruleURL, ROOT_SEPARATOR);
		}
		logger.debug("the process id will be called is : " + processID + ";output class name is : " + outClsArr.toString());
		this.root = root;
		this.processID = processID;
		this.retObjs = outClsArr;
	}
	
	public String getPlotId(){
		return this.root + this.processID;
	}
	private boolean notNullValues(String processID) {
		return processID!= null && !processID.equals(EMPTY) && processID.length() != 0;
	}

	private boolean vaildURL(String ruleURL) {
		if (notNullValues(ruleURL)
				&& StringUtils.indexOfAny(ruleURL, ROOT_SEPARATOR) >= 1) {
			return true;
		}
		return false;
	}
	
	public String toString() {
		String rtsp = RETURN_SEPARATOR + StringUtils.join(retObjs, ",");
		logger.debug(rtsp);
		return rtsp.equals(RETURN_SEPARATOR) ? this.root + "/" + this.processID
				: this.root + "/" + this.processID ;

	}
	
	public String getRoot() {
		return root;
	}

	public void setRoot(String root) {
		this.root = root;
	}

	public String getProcessID() {
		return processID;
	}

	public void setProcessID(String processID) {
		this.processID = processID;
	}

	public String[] getRetObjs() {
		return retObjs;
	}

	public void setRetObjs(String[] retObjs) {
		this.retObjs = retObjs;
	}
	
	public static void main(String[] arg) throws Exception{
		InputUrlAdapter iuap = new InputUrlAdapter("com.ccb.tfs.n2tfs/sample?return=Message");
		
		System.out.println(iuap.toString());
		
	}
}
